package com.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
 * xml文件操作类
 * 
 *
 */
public class XmlHelp {
	/**
	 * 搜索xml文件中属性attributeName的值为keyWord的所有二级节点
	 * @param xmlPath
	 * @param childEleName
	 * @param attributeName
	 * @param keyWord
	 * @return
	 */
	public static List search(String xmlPath,String childEleName,String attributeName,String keyWord){
		SAXBuilder builder=new SAXBuilder(false);
		List list=null;
		List l=new ArrayList();
		try {
			Document doc=builder.build(xmlPath);
			Element ele=doc.getRootElement();
			list=ele.getChildren(childEleName);
			for(int i=0;i<list.size();i++){
				Element e=(Element)list.get(i);
				if(e.getAttributeValue(attributeName).equals(keyWord)){
					l.add(e);
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return l;
	}
	
	
	/**
	 * 返回XML二级子节点列表
	 * @param xmlPath xml文件路径
	 * @param childEleName 子节点名称
	 * @return
	 */
	public static List findAllChildEle(String xmlPath,String childEleName){
		SAXBuilder builder=new SAXBuilder(false);
		List list=null;
		try {
			Document doc=builder.build(xmlPath);
			Element ele=doc.getRootElement();
			list=ele.getChildren(childEleName);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 修改一个二级子节点
	 * @param xmlPath xml文件路径
	 * @param childEleName 子节点名称
	 * @param id 子节点属性
	 * @param newValue 子节点属性
	 * @return
	 */
	public static boolean update1(String xmlPath,String childEleName,String id,String newValue){
		try   
		  {   
			  SAXBuilder   sb=new   SAXBuilder();   
			  Document doc=sb.build(new FileInputStream(xmlPath));
			  Element   root   =   doc.getRootElement();//获得根元素
			  List   childList   =   root.getChildren(childEleName);
			  if(childList!=null){
					for(int i=0;i<childList.size();i++){
						Element ele = (Element)childList.get(i);
						if(id.equals(ele.getAttributeValue("id"))){
							ele.setAttribute("value",newValue);
							childList.remove(i);
							childList.add(i, ele);
							break;
						}
					}
			  }   
			  XMLOutputter   xml=   new   XMLOutputter();   
			  xml.output(doc,new   FileOutputStream(xmlPath));   
		  }   
		  catch(Exception   e)   
		  {   
			  //System.out.println("error"+e.getMessage());
			  return false;
		  } 
		return true;
	}
	
	public static boolean update2(String xmlPath,String childEleName,String id,String newValue1,String newValue2){
		try   
		  {   
			  SAXBuilder   sb=new   SAXBuilder();   
			  Document doc=sb.build(new FileInputStream(xmlPath));
			  Element   root   =   doc.getRootElement();//获得根元素
			  List   childList   =   root.getChildren(childEleName);
			  if(childList!=null){
					for(int i=0;i<childList.size();i++){
						Element ele = (Element)childList.get(i);
						if(id.equals(ele.getAttributeValue("id"))){
							ele.setAttribute("value",newValue1);
							ele.setAttribute("remark", newValue2);
							childList.remove(i);
							childList.add(i, ele);
							break;
						}
					}
			  }   
			  XMLOutputter   xml=   new   XMLOutputter();   
			  xml.output(doc,new   FileOutputStream(xmlPath));   
		  }   
		  catch(Exception   e)   
		  {   
			  //System.out.println("error"+e.getMessage());
			  return false;
		  } 
		return true;
	}
	
	/**
	 * 删除一个二级子节点
	 * @param xmlPath xml文件路径
	 * @param childEleName 子节点名称
	 * @param id 子节点属性
	 * @return
	 */
	public static boolean delete(String xmlPath,String childEleName,String id){
		boolean b=false;
		try   
		  {   
			  SAXBuilder   sb=new   SAXBuilder();   
			  Document doc=sb.build(new FileInputStream(xmlPath));
			  Element   root   =   doc.getRootElement();//获得根元素
			  List   childList   =   root.getChildren(childEleName);
			  if(childList!=null){
					for(int i=0;i<childList.size();i++){
						Element ele = (Element)childList.get(i);
						if(id.equals(ele.getAttributeValue("id"))){
							childList.remove(i);
							b=true;
							break;
						}
					}
			  }   
			  XMLOutputter   xml=   new   XMLOutputter();   
			  xml.output(doc,new   FileOutputStream(xmlPath));   
		  }   
		  catch(Exception   e)   
		  {   
			  //System.out.println("error"+e.getMessage());
			  return false;
		  } 
		return b;
	}
	
	/**
	 * 在xml文件末尾插入一个二级子节点
	 * @param xmlPath xml文件路径
	 * @param childEleName 子节点名称
	 * @param newValue 子节点属性
	 * @return
	 */
	public static boolean add(String xmlPath,String childEleName,String newValue){
		  try   
		  {   
			  SAXBuilder   sb=new   SAXBuilder();   
			  Document doc=sb.build(new FileInputStream(xmlPath));
			  Element   root   =   doc.getRootElement();//获得根元素
			  List   childList   =   root.getChildren(childEleName);
			  Element   ele   =   new   Element(childEleName);//要添加的节点
			  ele.setAttribute("id",childList.size()+"");
			  ele.setAttribute("value", newValue);
			  childList.add(0,ele);   
			  XMLOutputter   xml=   new   XMLOutputter();   
			  xml.output(doc,new   FileOutputStream(xmlPath));   
		  }   
		  catch(Exception   e)   
		  {   
			  //System.out.println("error"+e.getMessage());
			  return false;
		  }
		  return true;
	  }
	
	public static boolean add2(String xmlPath,String childEleName,String newValue1,String newValue2){
		  try   
		  {   
			  SAXBuilder   sb=new   SAXBuilder();   
			  Document doc=sb.build(new FileInputStream(xmlPath));
			  Element   root   =   doc.getRootElement();//获得根元素
			  List   childList   =   root.getChildren(childEleName);
			  Element   ele   =   new   Element(childEleName);//要添加的节点
			  ele.setAttribute("id",childList.size()+"");
			  ele.setAttribute("value", newValue1);
			  ele.setAttribute("remark",newValue2);
			  childList.add(ele);   
			  XMLOutputter   xml=   new   XMLOutputter();   
			  xml.output(doc,new   FileOutputStream(xmlPath));   
		  }   
		  catch(Exception   e)   
		  {   
			  //System.out.println("error"+e.getMessage());
			  return false;
		  }
		  return true;
	  }
	
	public static String getXmlPath(String path){
		return ServletActionContext.getServletContext().getRealPath(path);
	}
	
}
